package userinfo

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
	"github.com/hugozhu/godingtalk"
	"myschools.me/dingtalk/config"
)

//UserInfoHandler 用户信息获取
func UserInfoHandler(c *gin.Context) {
	code := strings.TrimLeft(c.Param("code"), "/")
	client := godingtalk.NewDingTalkClient(config.Dingtalk.AppKey, config.Dingtalk.AppSecret)
	client.RefreshAccessToken()
	u, err := client.UserInfoByCode(code)
	if err != nil {
		fmt.Println(err.Error())
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data": u,
	})
}
